package com.shawn.baseappbygroovydsl.pop

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import com.bumptech.glide.Glide
import com.choryan.quan.videowzproject.extension.ExtensionView.animationBreath
import com.choryan.quan.videowzproject.extension.ExtensionView.hide
import com.choryan.quan.videowzproject.extension.ExtensionView.show
import com.shawn.baseappbygroovydsl.R
import com.shawn.baseappbygroovydsl.appInterface.IPopBtnActionListener
import com.shawn.baseappbygroovydsl.component.SoundPoolManager
import com.shawn.baseappbygroovydsl.ui.adapter.ChatContentListAdapter
import eee.drama.idea_chat.bean.MessageChatBean
import eee.drama.idea_chat.databinding.PopRedChatBinding

/**
 * @author Secret
 * @since 2023/6/1
 */
class PopRedChat(context: Context) : PopBase(context) {

    var popBtnActionListener: IPopBtnActionListener? = null
    private lateinit var mBind:PopRedChatBinding

    companion object {
        const val POP_RED_CHAT_GET = "pop_red_chat_get"
        const val POP_RED_CHAT_CLOSE = "pop_red_chat_close"
    }

    init {
        setBackgroundDrawable(null)
        initView()
    }

    private fun initView() {
        val view = LayoutInflater.from(context).inflate(eee.drama.idea_chat.R.layout.pop_red_chat, null)
        contentView = view
        mBind = PopRedChatBinding.bind(view)
    }

    private inner class GetAction : OnClickListener {
        override fun onClick(v: View?) {
            SoundPoolManager.showSound(R.raw.tap_button)

            popBtnActionListener?.onPopActionClick(POP_RED_CHAT_GET)
        }
    }

    private inner class CloseAction : OnClickListener {
        override fun onClick(v: View?) {
            SoundPoolManager.showSound(R.raw.tap_button)
            popBtnActionListener?.onPopActionClick(POP_RED_CHAT_CLOSE)
            dismiss()
        }
    }

    private fun refreshStyle0(item: MessageChatBean) {
        val bind = mBind.popChatStyle0
        bind.root.show()
        bind.ivOpen.setOnClickListener(GetAction())
        bind.ivClose.setOnClickListener(CloseAction())
        bind.tvName.text = item.name
        bind.ivOpen.animationBreath()
        Glide.with(context)
            .load(item.icon)
            .circleCrop()
            .into(bind.sivIcon)
    }

    private fun refreshStyle1(item: MessageChatBean) {
        val bind = mBind.popChatStyle1
        bind.root.show()
        bind.ivOpen.setOnClickListener(GetAction())
        bind.ivClose.setOnClickListener(CloseAction())
        bind.tvName.text = item.name
        bind.ivOpen.animationBreath()
        Glide.with(context)
            .load(item.icon)
            .circleCrop()
            .into(bind.sivIcon)
    }

    private fun refreshStyle2(item: MessageChatBean) {
        val bind = mBind.popChatStyle2
        bind.root.show()
        bind.ivOpen.setOnClickListener(GetAction())
        bind.ivClose.setOnClickListener(CloseAction())
        bind.tvName.text = item.name
        bind.ivOpen.animationBreath()
        Glide.with(context)
            .load(item.icon)
            .circleCrop()
            .into(bind.sivIcon)
    }

    fun show(parent: View, item: MessageChatBean) {
        mBind.popChatStyle0.root.hide()
        mBind.popChatStyle1.root.hide()
        mBind.popChatStyle2.root.hide()
        when (item.itemType) {
            ChatContentListAdapter.ITEM_TYPE_RED_STYLE_0 -> refreshStyle0(item)
            ChatContentListAdapter.ITEM_TYPE_RED_STYLE_1 -> refreshStyle1(item)
            ChatContentListAdapter.ITEM_TYPE_RED_STYLE_2 -> refreshStyle2(item)
        }
        showAtLocation(parent, Gravity.NO_GRAVITY, 0, 0)
    }

}